#include "ButtonTouchEffect.h"

void addButtonTouchEffect(ui::Widget* pWidget,
	ButtonTouchEffect effect,
	const std::function<void(Ref*, ui::Widget::TouchEventType)>& callback)
{
	CCASSERT(pWidget, "addButtonTouchEffect : pWidget is nullptr");

	pWidget->addTouchEventListener([effect, callback](Ref* pSender, ui::Widget::TouchEventType type)
	{
		Node* pNode = (Node*)pSender;

		if (ButtonTouchEffect::SINK == effect)
		{
			if (ui::Widget::TouchEventType::BEGAN == type)
			{
				pNode->setPosition(pNode->getPosition() + Vec2(5, -5));
			}
			else if (ui::Widget::TouchEventType::ENDED == type || ui::Widget::TouchEventType::CANCELED == type)
			{
				pNode->setPosition(pNode->getPosition() + Vec2(-5, 5));
			}
		}
		else if (ButtonTouchEffect::DARK == effect)
		{
			if (ui::Widget::TouchEventType::BEGAN == type)
			{
				pNode->setColor(Color3B(0xbb, 0xbb, 0xbb));
			}
			else if (ui::Widget::TouchEventType::ENDED == type || ui::Widget::TouchEventType::CANCELED == type)
			{
				pNode->setColor(Color3B(0xff, 0xff, 0xff));
			}
		}

		if (ui::Widget::TouchEventType::ENDED == type)
		{
			callback(pSender, type);
		}
	});

}